package com.fms.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fms.common.vo.Result;
import com.fms.sys.dto.PasswordCategoryDTO;
import com.fms.sys.dto.PasswordInfoDTO;
import com.fms.sys.entity.PasswordCategory;
import com.fms.sys.entity.PasswordInfo;
import com.fms.sys.service.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.fms.common.utils.JwtUtils;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 密码管理 前端控制器
 * </p>
 *
 * @author fjt
 * @since 2024-04-21
 */
@RestController
@RequestMapping("/password")
@Validated
public class PasswordController {
    @Autowired
    private PasswordService passwordService;

    @GetMapping("/categories")
    public Result<List<PasswordCategoryDTO>> listCategories() {
        try {
            return Result.success(passwordService.listCategories());
        } catch (Exception e) {
            return Result.fail("获取密码分类失败：" + e.getMessage());
        }
    }

    @PostMapping("/category")
    public Result<?> addCategory(@Valid @RequestBody PasswordCategory category) {
        try {
            category.setUserId(JwtUtils.getCurrentUserId());
            return Result.success(passwordService.addCategory(category), "添加分类成功");
        } catch (Exception e) {
            return Result.fail("添加分类失败：" + e.getMessage());
        }
    }

    @PutMapping("/category")
    public Result<?> updateCategory(@Valid @RequestBody PasswordCategory category) {
        try {
            category.setUserId(JwtUtils.getCurrentUserId());
            passwordService.updateCategory(category);
            return Result.success("更新分类成功");
        } catch (Exception e) {
            return Result.fail("更新分类失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/category/{id}")
    public Result<?> deleteCategory(@PathVariable Long id) {
        try {
            passwordService.deleteCategory(id);
            return Result.success("删除分类成功");
        } catch (Exception e) {
            return Result.fail("删除分类失败：" + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Map<String, Object>> listPasswords(
            @RequestParam(defaultValue = "1") Long pageNo,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long categoryId) {
        try {
            Page<PasswordInfo> page = new Page<>(pageNo, pageSize);
            IPage<PasswordInfoDTO> passwordPage = passwordService.listPasswords(page, keyword, categoryId);

            Map<String, Object> data = new HashMap<>();
            data.put("total", passwordPage.getTotal());
            data.put("rows", passwordPage.getRecords());

            return Result.success(data);
        } catch (Exception e) {
            return Result.fail("获取密码列表失败：" + e.getMessage());
        }
    }

    @PostMapping("/add")
    public Result<?> addPassword(@Valid @RequestBody PasswordInfo password) {
        try {
            password.setUserId(JwtUtils.getCurrentUserId());
            return Result.success(passwordService.addPassword(password), "添加密码成功");
        } catch (Exception e) {
            return Result.fail("添加密码失败：" + e.getMessage());
        }
    }

    @PutMapping("/update")
    public Result<?> updatePassword(@Valid @RequestBody PasswordInfo password) {
        try {
            password.setUserId(JwtUtils.getCurrentUserId());
            passwordService.updatePassword(password);
            return Result.success("更新密码成功");
        } catch (Exception e) {
            return Result.fail("更新密码失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<?> deletePassword(@PathVariable Long id) {
        try {
            passwordService.deletePassword(id);
            return Result.success("删除密码成功");
        } catch (Exception e) {
            return Result.fail("删除密码失败：" + e.getMessage());
        }
    }
}
